<template>
	<view class="search">
		<Header title='搜索' color='black' :iconblack='true'/>
		
		<view class="scrollContainer" v-show="loading">
			<scroll-view scroll-y="true" >
				<view class="search-search">
					<text class="iconfont">&#xe63a;</text>
					<input type="text" placeholder="搜什么搜自己唱"  v-model="hotWord" @confirm='handleSearch(hotWord)' @input='handleInputEvent'/>
					<text v-show="searchType !== 1" class="iconfont close" @tap='handleSearchClear'>&#xe601;</text>
				</view>
				
				<block v-if="searchType === 1">
					<view class="search-history">
						<view class="search-history-head">
							<text>历史记录</text>
							<text class="iconfont" @tap='clearSearchHistory'>&#xe645;</text>
						</view>
						<view class="search-history-list">
							<view v-for="(item,ind) in searchHistory" :key='ind'  @tap='handleToInput(item)'>
								{{item}}
							</view>
						</view>
					</view>
					
					<view class="search-hot">
						<view class="search-hot-head">
							热搜榜
						</view>
						<view class="search-hot-item" v-for="(item,ind) in hotWordList" :key='ind' @tap='handleToInput(item.searchWord)'>
							<view class="search-hot-item-top">
								{{ind + 1}}
							</view>
							<view class="search-hot-item-word">
								<view>
									{{item.searchWord}} <image :src="item.iconUrl" mode="aspectFit"></image>
								</view>
								<view>
									{{item.content}}
								</view>
							</view>
							<text class="search-hot-item-count">{{item.score}}</text>
						</view>
					</view>
				</block>
				
				<block v-else-if="searchType === 2">
					<view class="search-result">
						
						<view class="search-result-item" v-for="(item,ind) in searchResultList" :key='ind' @tap='handleToDetail(item.id)'>
							<view class="search-result-item-word">
								<view>
									{{item.name}}
								</view>
								<view>
									{{item.artists[0].name}} - {{item.album.name}}
								</view>
							</view>
							<text class="iconfont">&#xe610;</text>
						</view>
					</view>
				</block>
				
				<block v-else-if="searchType === 3">
					<view class="search-suggest">
						<view class="search-suggest-head" @tap='handleToInput(hotWord)'>
							搜索“{{hotWord}}”
						</view>
						<view class="search-suggest-item" v-for="(item,ind) in searchSuggestList" :key='ind' @tap='handleToInput(item.keyword)'>
							<text class="iconfont">&#xe63a;</text>{{item.keyword}}
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {baseUrl} from '../../common/config.js'
	
	export default {
		data() {
			return {
				loading:true,
				hotWordList:[],
				hotWord:'',
				searchHistory:[],
				searchType:1,
				searchResultList:[],
				searchSuggestList:[]
			}
		},
		onLoad() {
			this.getSearchHot()
			this.getSearchHistoryStorage()
		},
		methods: {
			getSearchHot(){
				const url = `${baseUrl}/search/hot/detail`
				uni.$u.http.get(url).then((res)=>{
					if(res.data.code == 200) {
						this.hotWordList = res.data.data
					}
				})
			},
			handleSearchClear(){
				this.hotWord = ''
				this.searchType = 1
			},
			handleToDetail(id){
				uni.navigateTo({
					url: '/pages/detail/detail?songId='+ id
				});
			},
			getSearchByWord(word){
				const url = `${baseUrl}/search?keywords=${word}`
				uni.$u.http.get(url).then((res)=>{
					console.log(res);
					if(res.data.code == 200) {
						this.searchResultList = res.data.result.songs
						this.searchType = 2
					}
				})
			},
			getSearchSuggestByWord(word){
				const url = `${baseUrl}/search/suggest?type=mobile&keywords=${word}`
				uni.$u.http.get(url).then((res)=>{
					if(res.data.code == 200) {
						this.searchSuggestList = res.data.result.allMatch
					}
				})
			},
			handleToInput(word){
				this.hotWord = word
				this.handleSearch(word)
			},
			handleInputEvent(event){
				uni.$u.debounce(()=>{
					this.handleToSuggest(event)
				}, 500)
			},
			handleSearch(word){
				this.searchHistory.unshift(word)
				this.searchHistory = [...new Set(this.searchHistory)]
				if(this.searchHistory.length > 10) {
					this.searchHistory.length = 10
				}
				uni.setStorage({
					key:'SEARCHHISTORY',
					data:this.searchHistory
				})
				this.getSearchByWord(word)
			},
			getSearchHistoryStorage(){
				uni.getStorage({
					key:'SEARCHHISTORY',
					success:(res)=>{
						this.searchHistory = res.data
					}
				})
			},
			clearSearchHistory(){
				uni.removeStorage({
				    key: 'SEARCHHISTORY',
				    success:(res)=>{
				      this.searchHistory = []
				    }
				});
			},
			handleToSuggest(event){
				let val = event.detail.value
				if(val) {
					this.searchType = 3
					this.getSearchSuggestByWord(val)
				} else {
					this.searchType = 1
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search-search {
		display: flex;
		align-items: center;
		height: 70rpx;
		margin: 70rpx 30rpx 30rpx 30rpx;
		background: #f7f7f7;
		border-radius: 50rpx;
		
		& text {
			margin: 0 26rpx;
		}
		
		& .close {
			font-size: 20rpx;
		}
		
		& input {
			flex: 1;
			font-size: 26rpx;
		}
	}

	.search-history {
		margin: 0 30rpx 50rpx 30rpx;
		font-size: 26rpx;
		
		&-head {
			display: flex;
			justify-content: space-between;
			margin-bottom: 36rpx;
		}
		
		&-list {
			display: flex;
			flex-wrap: wrap;
			
			& view {
				padding: 16rpx 28rpx;
				border-radius: 40rpx;
				margin-right: 30rpx;
				margin-bottom: 30rpx;
				background-color: #f7f7f7;
			}
		}
	}

	.search-hot {
		margin: 0 30rpx;
		font-size: 26rpx;
		
		&-head {
			margin-bottom: 36rpx;
		}
		
		&-item {
			display: flex;
			align-items: center;
			margin-bottom: 58rpx;
			
			&-top {
				color: #fb2222;
				width: 60rpx;
				margin-left: 8rpx;
			}
			
			&-word {
				flex: 1;
				
				& view:nth-child(1) {
					font-size: 30rpx;
					color: black;
				}
				
				& view:nth-child(2) {
					font-size: 24rpx;
					color: #878787;
				}
				
				& image {
					width: 48rpx;
					height: 22rpx;
				}
			}
		
			&-count {
				color: #878787;
			}
		}
	}

	.search-result {
		border-top: 2rpx #e4e4e4 solid;
		padding: 30rpx;
		
		&-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-bottom: 30rpx;
			margin-bottom: 30rpx;
			border-bottom: 2rpx #e4e4e4 solid;
			
			&-word {
				& view:nth-child(1) {
					font-size: 28rpx;
					color: #235790;
					margin-bottom: 12rpx;
				}
				
				& view:nth-child(2) {
					font-size: 22rpx;
					color: #898989;
				}
			}
			
			& text {
				font-size: 50rpx;
			}
		}
	}

	.search-suggest {
		border-top: 2rpx #e4e4e4 solid;
		padding: 30rpx;
		font-size: 26rpx;
		
		&-head {
			color: #4574a5;
			margin-bottom: 74rpx;
		}
		
		&-item {
			color: #5d5d5d;
			margin-bottom: 74rpx;
			
			& text {
				color: #bdbdbd;
				margin-right: 28rpx;
				position: relative;
				top: 3rpx;
			}
		}
	}
</style>
